/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-12-2
 * @Time  : 下午3:03
 * @brief :
 */


P.createPage({
    _data:{
        status:0,
        date_type:0,
        product_type:0
    },

    _create:function(){
        var Pagination = require('common:widget/ui/pagination/pagination.js').Pagination,
            Dialog = require('common:widget/ui/dialog/dialog.js').Dialog,
            util = require('common:widget/ui/libs/util.js').util,
            $page = $('.page-assetsterm'),
            $recordBlock = $page.find('.record-block'),
            dialog = new Dialog({
                cancelBtnText:'关闭'
            }),
            me = this,
            pagination;


        //初始化页码
        pagination = new Pagination({
            container:$recordBlock.find('.record-pagination'),
            perCount:10,
            cur:1,
            onRefresh:function(curPage,callback){
                $.getJSON('/user/gettermlist',{
                    'ps':10,
                    'pn':curPage,
                    'status':me.data('status'),
                    'date_type':me.data('date_type'),
                    'product_type':me.data('product_type')
                },function(ret,status){
                    if(ret.error_no == 0 && ret.data){
                        callback(ret.data.count);
                        me._createTableList(ret.data.list);
                    }
                });
            }
        });


        this.data({
            '$page':$page,
            '$recordBlock':$recordBlock,
            '$timeTab':$recordBlock.find('.time-tab a'),
            '$statusTab':$recordBlock.find('.status-tab a'),
            '$productTab':$recordBlock.find('.product-tab a'),
            '$recordContainer':$recordBlock.find('.record-table tbody'),
            'pagination':pagination,
            'util':util,
            'dialog':dialog
        })
    },


    _init:function(){
        var $page = this.data('$page'),
            $timeTab = this.data('$timeTab'),
            $statusTab = this.data('$statusTab'),
            $productTab = this.data('$productTab'),
            pagination = this.data('pagination'),
            $recordContainer = this.data('$recordContainer'),
            me = this;

        $timeTab.click(function(){
            $timeTab.removeClass('active');
            $(this).addClass('active');
            me.data('date_type',$(this).attr('data-date-type'));
            pagination.initPage(1);
            $recordContainer.html('');
        });

        $productTab.click(function(){
            $productTab.removeClass('active');
            $(this).addClass('active');
            me.data('product_type',$(this).attr('data-product-type'));
            pagination.initPage(1);
            $recordContainer.html('');
        });

        $statusTab.click(function(){
            var status = $(this).attr('data-status');
            $statusTab.removeClass('active');
            $(this).addClass('active');
            me.data('status',status);
            pagination.initPage(1);
            $recordContainer.html('');
            status==0?$page.find('.term-opration').show():$page.find('.term-opration').hide();
        });
    },

    //购买列表
    _createTableList:function(list){
        var status = this.data('status'),
            util = this.data('util'),
            me = this;
        this.data('$recordContainer').html((function(){
            var html = "";
            $.each(list,function(k,v){
                html+='' +
                    '<tr>' +
                    '<td>'+ v.serial_id+'</td>' +
                    '<td>'+util.toTime(v.time*1000)+'</td>' +
                    '<td>'+ P.productMap[v.pid]+'</td>' +
                    '<td>'+ mathUtil.formatAmount(v.amount,4)+'</td>' +
                    '<td>'+util.toTime(v.end_time*1000)+'</td>' +
                    (status?'<td>￥'+ mathUtil.formatAmount(v.actual_profit,4)+'</td>':
                        '<td>￥'+ mathUtil.formatAmount(v.expect_profit,4)+'</td>')+
                    (me.data('status')==0?
                    '<td>' +
                    '<select data-id="'+ v.id+'" class="complex-status">' +
                    '<option value="0" '+(v.compound_status==0?'selected="selected"':'')+'>自动赎回</option>' +
                    '<option value="1" '+(v.compound_status==1?'selected="selected"':'')+'>本金复投</option>' +
                    '<option value="2" '+(v.compound_status==2?'selected="selected"':'')+'>本息复投</option>' +
                    '</select>' +
                    '</td>':'') +
                    '</tr>';
            });
            return html;
        })());
        this._bindEvent();
    },

    _bindEvent:function(){
        var dialog = this.data('dialog');
        this.data('$recordContainer').find('.complex-status').on('change',function(){
            var record_id = $(this).attr('data-id'),
                complex_status = $(this).val();

            $.post('/user/setcomplexstatus',{
                record_id:record_id,
                complex_status:complex_status
            },function(ret,status){
                 if(ret.error_no == 0){
                     dialog.show('修改复投模式成功');
                 }else{
                     dialog.show('修改复投模式失败');
                 }
            },'json');
        });
    }
});